package edu.ynu.se.xiecheng.achitectureclass.project.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;


@Slf4j
public class HttpUtils {
    private static final RestTemplate restTemplate = new RestTemplate();

    public static <T> T postJson(String url, Object requestBody, Class<T> responseType) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON); // 使用 Spring 的 APPLICATION_JSON

        HttpEntity<Object> entity = new HttpEntity<>(requestBody, headers);

        ResponseEntity<T> response = restTemplate.postForEntity(
                url,
                entity,
                responseType
        );

        log.debug("Response body: {}", response.getBody());
        return response.getBody();
    }
}